// pages/login/index.js
const loginX=require("../controllers/ControllerX").controller.login;
const getCode=require("../controllers/ControllerX").controller.getCode;
const configMessage=require("../controllers/ControllerX").controller.configMessage;
const Controller=require('../controllers/ControllerX').controller
const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src1:" https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/my/img/my_but_15.png",
    src2:" https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/my/img/my_but_26.png",
    select_src:" https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/image/my/img/my_but_15.png",
    state:"1",
    select:false,
    notice_state:false,
    dialogShow:false,
    buttons: [{text: '取消'}, {text: '确定'}],
    phone:'',
    code:"",
    binded:false,
    time:"获取验证码",
    hotPhone:"123456789"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getConfigMessage();
    if(wx.getStorageSync('token')){

    }else{
      this.GetLogin()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // },
  // 点击选中
  access(event){
    let state=event.currentTarget.dataset.state;
    if(state=="1"){
      this.setData({
        select_src:this.data.src2,
        state:"0",
        select:true
      })
    }else{
      this.setData({
        select_src:this.data.src1,
        state:"1",
        select:false
      })
    }
  },
  //修改phone值
  getPhoneValue(e){
    this.setData({
      phone:e.detail.value
    })
  },
  //修改code值
  getCodeValue(e){
    this.setData({
      code:e.detail.value
    })
  },
  // 获取验证码
  get_code(){
      var params={
        mobile:this.data.phone,
        type:"Login",
        sendType:2
      }
      if(!params.mobile){
        wx.showToast({
          icon:"none",
          title: '请填写手机账号',
          duration:2000
        })
      }else{
        getCode(params).then(res=>{
          if(res.code==1){
            if(this.data.binded==false){
              this.setData({
                binded:true,
                time:60
              })
              let timer=setInterval(() => {
                this.setData({
                  time:this.data.time-1
                })
                if(this.data.time<=0){
                  this.setData({
                    time:"获取验证码",
                    binded:false
                  })
                  clearInterval(timer);
                }
              }, 1000);
            }
            wx.showToast({
              icon:"none",
              title: '验证码已发送'
            })
          }else{
  
          }
        })
      }
      
  },
  // 登录
  login(){
    if(this.data.select==true){
      var that=this;
      var params={
        mobile:this.data.phone,
        openId:wx.getStorageSync('openId'),
        code:this.data.code
      }
      // if(!this.data.phone){
      //   this.setData({
      //     notice_state:true
      //   });
      //   setTimeout(function(){
      //     that.setData({
      //       notice_state:false
      //     });
      //   },3000)
      // };

      if(!params.mobile){
        wx.showToast({
          icon:"none",
          title: '请填写手机账号',
          duration:2000
        })
      }else if(!params.code){
        wx.showToast({
          icon:"none",
          title: '请填写验证码',
          duration:2000
        })
      }else{
         //调用登录api
      loginX(params).then(res=>{
        
        wx.setStorageSync('token', res.data.sessionId);
        wx.setStorageSync('mobile', this.data.phone);
        if(res.data.shop){
          wx.setStorageSync('accountId', res.data.shop.accountId);
        }
        wx.navigateTo({
          url: '/pages/login/select_store/index',
        })
      })
      }
    }
  },

  //登录请求
  GetLogin(flag) {
    if (flag == undefined || flag) {
      wx.showLoading({
        title: '登录中...',
        mask: true
      });
    }
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        Controller.appletGetOpenId({
          code: res.code
        }).then(result => {
          if(result.data.sessionKey){
            this.globalData.sessionKey=result.data.sessionKey
          }
          if(result.data.sessionId){
            wx.setStorageSync('token', result.data.sessionId)
          }
          // this.globalData.openId=result.data.openId
          wx.setStorageSync('openId', result.data.openId)
          if (this.tokenReadyCallback) {
            this.tokenReadyCallback(result);
          }
          
          if (flag == undefined || flag) {
            wx.hideLoading();
          }
        });
        
      }
    });
  },
  // 打开对话框
  openConfirm: function () {
    this.setData({
        dialogShow: true
    })
  },
  // 关闭对话框
  tapDialogButton(e) {
    let state=e.detail.index;
    if(state==1){
      this.doPhone();
    }
      this.setData({
          dialogShow: false
      })
  },
  //跳转入驻页
  jumpToSettlement(){
    wx.navigateTo({
      url: '/pages/settlement/index',
    })
  },

  //跳转协议页
  jumpToAgreement(e){
    let state=e.currentTarget.dataset.state;
    if(state=='1'){
      wx.navigateTo({
        url: '/pages/login/pri_ agreement/index',
      })
    }else{
      wx.navigateTo({
        url: '/pages/login/serve_ agreement/index',
      })
    }
  },

  //获取客服热线
  getConfigMessage(){
    configMessage({no:2}).then(res=>{
      this.setData({
        hotPhone:res.data.value
      })
    })
  },

  //拨打客服电话
  doPhone(){
    wx.makePhoneCall({
      phoneNumber: this.data.hotPhone,
    })
  }


})